<?php
/**
 * Created by JetBrains PhpStorm.
 * User: GJN
 * Date: 5/10/13
 * Time: 1:59 AM
 */
session_start();
/*
 * Để sử dụng session cần gọi session_start() ngay trước khi kiểm tra $_SESSION
 */
session_destroy(); //sử dụng hàm này để xóa session

if (isset($_SESSION['username']) || isset($_COOKIE['username'])) {
    if (isset($_SESSION['username']))
        echo 'You already loged in by Username: ' . $_SESSION['username'];
    else echo 'You already loged in by Username: ' . $_COOKIE['username'];

    foreach ($_COOKIE as $k => $v) {
        echo $k, ': ', $v, '<br/>';
    }
//    setcookie('username', '', time() - 10);
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (trim($_POST['user']) != '' && trim($_POST['pass']) != '') {
        echo 'Username: ', $_POST['user'], '<br/>';
        echo 'Password: ', $_POST['pass'], '<br/>';
        $_SESSION['username'] = $_POST['user'];
        if (!empty($_POST['save'])) {
            $expire = time() + 60 * 60 * 24 * 7;
            setcookie('username', $_POST['user'], $expire, '/absolute/');
        }
    } else {
        echo 'You need to fill out both of 2 fields.<br/>';
    }
} else {
    ?>
    <form action="03-test.php" method="post">
        Username: <input type="text" name="user"/><br/>
        Password: <input type="password" name="pass"/><br/>
        <input type="checkbox" name="save" value="yes"/>
        <input type="submit" value="Log in"/>
    </form>

<?php
}
?>